<?php
//
//	file: includes/attach/class_attach_posting.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.3 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt !');
}

class attach_posting extends attach_parent
{
	var $forum_id;
	var $topic_id;
	var $post_id;

	function attach_posting()
	{
		parent::attach_parent(PAGE_POSTING);
	}

	function start()
	{
		global $refresh;

		$refresh |= $this->action();
		return true;
	}

	function set_form()
	{
		global $template;
		global $mode, $forum_id, $topic_id, $post_id;
		global $set_menu;

		$this->forum_id = intval($forum_id);
		$this->topic_id = intval($topic_id);
		parent::init();
		$this->process($mode, $post_id);
		if ( $this->is_active )
		{
			$set_menu++;
		}
		$template->set_switch('attachment_form', $this->is_active);
		return true;
	}

	function topic_fields(&$fields)
	{
		$fields[] = 't.topic_attachment';
		return true;
	}

	function post_fields(&$fields)
	{
		$fields[] = 'p.post_attachment';
		return true;
	}

	function validate()
	{
		global $user;
		global $mode, $topic_id, $post_id, $post_data, $post_info;

		$user_id = ($mode == 'reply') || ($mode == 'newtopic') ? $user->data['user_id'] : $post_data['poster_id'];
		$post_attachment = $post_info['post_attachment'];
		$delete_topic = ($mode == 'delete') && $post_data['first_post'] && $post_data['last_post'];

		$this->user_id = intval($user_id);
		$this->post_id = intval($post_id);
		$this->topic_id = intval($topic_id);
		switch ( $mode )
		{
			case 'editpost':
			case 'newtopic':
			case 'reply':
				if ( $this->is_active )
				{
					$link = array(
						'post_id' => (int) $post_id,
						'privmsgs_id' => 0,
						'user_id' => (int) $user_id,
					);
					if ( $this->submit_all($link) )
					{
						$this->resync($post_id);
					}
				}
				break;

			case 'delete':
				if ( $post_id && $post_attachment )
				{
					if ( $this->delete_all($post_id) && !$delete_topic )
					{
						$this->resync();
					}
				}
				break;
		}
		return true;
	}

	function preview()
	{
		global $mode, $post_id;
		global $config;

		if ( $this->is_active )
		{
			if ( !$this->processed )
			{
				$this->process($mode, $post_id, true);
			}
			if ( !empty($this->data) )
			{
				$preview = new attach_display();
				$preview->data[0] = &$this->data;
				$preview->display_main();
				$config->globals['mod_attachment_CH']['extensions']->read();
				$preview->display(0, 'preview');
				unset($preview);
			}
		}
	}

	// @private
	function get_auth()
	{
		global $user, $config, $forums;

		$auth_names = array('auth_edit', 'auth_delete', 'auth_mod', 'auth_attachments');
		$count_auth_names = count($auth_names);
		$this->is_auth = array();
		for ( $i = 0; $i < $count_auth_names; $i++ )
		{
			$this->is_auth[ $auth_names[$i] ] = $user->auth(POST_FORUM_URL, $auth_names[$i], $this->forum_id);
		}
	}

	// @private
	function get_attachments($post_id)
	{
		global $db;

		$sql = 'SELECT a.post_id, d.*
					FROM ' . ATT_LINKS_TABLE . ' a, ' . ATT_DESC_TABLE . ' d
					WHERE d.attach_id = a.attach_id
						AND a.privmsgs_id = 0
						AND a.post_id = ' . intval($post_id) . '
					ORDER BY d.filetime DESC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$attachments = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$attachments[ intval($row['post_id']) ][ intval($row['attach_id']) ] = $row;
		}
		$db->sql_freeresult($result);
		return $attachments;
	}

	// @private
	function get_rules_id()
	{
		return $this->forum_id;
	}

	// @private
	function check_extension_auth($auth_cache)
	{
		global $config;
		return $config->globals['mod_attachment_CH']['extensions']->auth($auth_cache, $this->forum_id);
	}

	// @private
	function check_user_quota()
	{
		global $error, $error_msg;
		global $db, $config, $user;

		$this->get_quota_limits($user->data, PAGE_INDEX);
		if ( doubleval($config->globals['mod_attachment_CH']['config']->data['upload_filesize_limit']) )
		{
			$sql = 'SELECT SUM(d.filesize) AS sum_filesize
						FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a
						WHERE d.attach_id = a.attach_id
							AND a.user_id = ' . intval($user->data['user_id']) . '
							AND a.privmsgs_id = 0';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$total_filesize = ($row = $db->sql_fetchrow($result)) ? doubleval($row['sum_filesize']) : 0;
			$db->sql_freeresult($result);

			if ( ($total_filesize + $this->filesize) > doubleval($config->globals['mod_attachment_CH']['config']->data['upload_filesize_limit']) )
			{
				$units = array();
				$unit = size_get_units(doubleval($config->globals['mod_attachment_CH']['config']->data['upload_filesize_limit']), $units);
				$allowed_filesize = round(doubleval($config->globals['mod_attachment_CH']['config']->data['upload_filesize_limit']) / $unit, 2);
				$size_lang = $units[$unit];
				_error(sprintf($user->lang('User_upload_quota_reached'), $allowed_filesize, $size_lang));
			}
		}
	}

	function get_mode($mode)
	{
		if ( ($mode == 'quote') || ($mode == 'reply') )
		{
			$mode = 'reply';
			$this->post_id = 0;
		}
		if ( $mode == 'newtopic' )
		{
			$this->post_id = 0;
		}
		return $mode;
	}

	function delete_all($post_id)
	{
		global $db;

		$found = false;
		if ( !intval($post_id) )
		{
			return;
		}

		// read all physical name
		$sql = 'SELECT d.attach_id, d.physical_filename, d.thumbnail
					FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a
					WHERE d.attach_id = a.attach_id
						AND a.post_id = ' . intval($post_id);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$files = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$files[ intval($row['attach_id']) ] = array('physical_filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail']);
		}
		$db->sql_freeresult($result);

		if ( !empty($files) )
		{
			// delete the links to the posts
			$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
						WHERE post_id = ' . intval($post_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// is the file shared ? if so, don't delete it
			$sql = 'SELECT DISTINCT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE attach_id IN(' . implode(', ', array_keys($files)) . ')';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				unset($files[ intval($row['attach_id']) ]);
			}
			$db->sql_freeresult($result);
		}

		// delete the files on the ftp
		if ( !empty($files) )
		{
			if ( $this->handler->open_channel() )
			{
				foreach ($files as $attach_id => $file )
				{
					$this->handler->unlink($file['physical_filename'], $file['thumbnail']);
				}
				$this->handler->close_channel();
			}
			$attach_ids = array_keys($files);
			unset($files);

			// delete the files description
			$sql = 'DELETE FROM ' . ATT_DESC_TABLE . '
						WHERE attach_id IN(' . implode(', ', $attach_ids) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
			unset($attach_ids);

			$found = true;
		}
		return $found;
	}

	function delete_one($post_id, $attach_id)
	{
		global $db;

		if ( empty($this->data) || !isset($this->data[ intval($attach_id) ]) )
		{
			return;
		}

		// remove the link to the post if exists
		if ( intval($post_id) )
		{
			$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
						WHERE post_id = ' . intval($post_id) . '
							AND attach_id = ' . intval($attach_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// is the file shared ?
		$found = false;
		if ( !$this->data[ intval($attach_id) ]['pending'] )
		{
			$sql = 'SELECT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE attach_id = ' . intval($attach_id) . '
						LIMIT 1';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$found = $db->sql_numrows($result);
			$db->sql_freeresult($result);
		}

		// remove the file if not shared
		if ( !$found )
		{
			if ( $this->handler->open_channel() )
			{
				$this->handler->unlink($this->data[ intval($attach_id) ]['physical_filename'], $this->data[ intval($attach_id) ]['thumbnail']);
				$this->handler->close_channel();
			}

			// delete desc
			$sql = 'DELETE FROM ' . ATT_DESC_TABLE . '
						WHERE attach_id = ' . intval($attach_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// adjust the post if exists
		if ( intval($post_id) && !$this->data[ intval($attach_id) ]['pending'] )
		{
			$sav_data = $this->data;
			unset($sav_data[ intval($attach_id) ]);
			$no_more = empty($sav_data);
			unset($sav_data);

			if ( $no_more && intval($post_id) )
			{
				$sav_data = $this->data;
				$this->data = array();
				$this->resync($post_id);
				$this->data = $sav_data;
			}
		}
	}

	function resync($post_id=0)
	{
		global $db;

		$found = empty($this->data) ? 0 : 1;
		if ( intval($post_id) )
		{
			$sql = 'UPDATE ' . POSTS_TABLE . '
						SET post_attachment = ' . intval($found) . '
						WHERE post_id = ' . intval($post_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		if ( intval($this->topic_id) )
		{
			$sql = 'SELECT post_attachment
						FROM ' . POSTS_TABLE . '
						WHERE topic_id = ' . intval($this->topic_id) . '
						ORDER BY post_attachment DESC
						LIMIT 1';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$topic_attachment = ($row = $db->sql_fetchrow($result)) ? intval($row['post_attachment']) : 0;
			$db->sql_freeresult($result);

			$sql = 'UPDATE ' . TOPICS_TABLE . '
						SET topic_attachment = ' . intval($topic_attachment) . '
						WHERE topic_id = ' . intval($this->topic_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}
}

?>